package com.example.studentgrades.controller;

import com.example.studentgrades.entity.Grade;
import com.example.studentgrades.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/grades")
public class GradeController {
    @Autowired
    private GradeService gradeService;

    @GetMapping("/listAll")
    public ResponseEntity<List<Grade>> listAllGrades() {
        List<Grade> grades = gradeService.findAll();
        return ResponseEntity.ok(grades);
    }

    @GetMapping("/getByCourseId/{courseId}")
    public ResponseEntity<List<Grade>> getGradesByCourseId(@PathVariable Integer courseId) {
        List<Grade> grades = gradeService.findByCourseId(courseId);
        return ResponseEntity.ok(grades);
    }

    @PostMapping("/update")
    public ResponseEntity<Void> updateGrade(@RequestBody Grade grade) {
        gradeService.save(grade);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/getByRange/{minScore}/{maxScore}")
    public ResponseEntity<List<Grade>> getGradesByRange(@PathVariable BigDecimal minScore, @PathVariable BigDecimal maxScore) {
        List<Grade> grades = gradeService.findByScoreRange(minScore, maxScore);
        return ResponseEntity.ok(grades);
    }
}
